#include "vcd-start-window.h"
#include "vcd-logined-window.h"
#include "vcd-message-dialog.h"
#include "vcd-passwd-window.h"

#include "../vcd-common-api/vcd-conf/vcd-conf.h"
#include "../vcd-common-api/vcd-download.h"
#include "../vcd-common-api/vcd-common.h"
#include "../vcd-common-api/vcd-log.h"
#include "../vcd-common-api/vcd-css.h"
#include "../vcd-common-api/vcd-json-parser.h"
#include "../vcd-common-api/vcd-get-login-ticket.h"
#include "../vcd-common-api/vcd-crypto.h"
#include "../vcd-common-api/vcd-send-heartbeat.h"
#include "vcd-setting-window/vcd-setting-window.h"
#include "vcd-setting-window/setting-network/vcd-node.h"
#include "vcd-setting-window/setting-network/vcd-wire.h"
#include "vcd-setting-window/setting-network/vcd-wifi.h"
#include "../vcd-linux-ui/vcd-webkit.h"
#include "vcd-logined-window.h"

struct start_window *g_start_window = NULL;
int g_timer = 0;

int g_timeout_ip_node = 0;
int g_timeout_wait_ip = 0;
int g_icon_press_state = 0;

static gboolean get_ip_node(void);
static void cb_button_quit(gpointer s);
static void cancel_auto_login(void);
static void show_wait(void);
static void cb_auto_login(void);
static void cb_remember_passwd(void);
static void cb_button_login(void);
static void cb_entry_icon_press(GtkWidget *entry);
static void prepare_local_model(void);
static void set_local_model_status(gboolean is_local_model);
static void login_as_local_model(void);
static gboolean if_support_local_model(void);

static void cb_shutdown_system(gpointer s)
{
#ifdef __linux__
    system("init 0");
#endif
}

static void cb_button_quit(gpointer s)
{
    //for generate *.gcda //system("init 0");
    message_diag_context *t = g_malloc0(sizeof(message_diag_context));
    g_return_if_fail(t != NULL);
    t->btn_yes = cb_shutdown_system;
    t->btn_no = NULL;
    t->info = "您将要关机，是否继续？";
    t->user_data = s;
    vcd_message_dialog(t);
    g_free(t);
    t = NULL;
}

static
void cb_auto_login(void)
{
    struct vcd_conf* tmp_vcd_conf = g_vcd_conf;

    if (gtk_toggle_button_get_active((GtkToggleButton *)g_start_window->auto_login))
    {
        gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->remember_passwd, TRUE);
        tmp_vcd_conf->enable_autologin = 1;
    } else
    {
        tmp_vcd_conf->enable_autologin = 0;
    }
    update_vcd_conf();
}

static
void cb_remember_passwd(void)
{
    if (!gtk_toggle_button_get_active((GtkToggleButton *)g_start_window->remember_passwd))
        gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->auto_login, FALSE);
}

static
void cancel_auto_login(void)
{
    gtk_widget_destroy(g_start_window->show_wait_win);
    g_source_remove(g_timeout_wait_ip);
}

static void cb_local_model(void)
{
    gboolean bStatus = FALSE;

    if (gtk_toggle_button_get_active((GtkToggleButton *)g_start_window->local_model)) {
        bStatus = TRUE;
    }

    set_local_model_status(bStatus);
}

static
gboolean wait_ip(void)
{
    char ip_str[32] = {0};
    if (get_local_ip(ip_str) == 0) {
        g_source_remove(g_timeout_wait_ip);
        gtk_widget_destroy(g_start_window->show_wait_win);
        cb_button_login();
    } else {
        g_timer++;
        if (g_timer > 25) {
            gtk_widget_destroy(g_start_window->show_wait_win);
            message_box(" 自动连接失败（无网络连接）! ");
            g_source_remove(g_timeout_wait_ip);
        }
    }
    return TRUE;
}

static
void show_wait(void)
{
    GdkScreen *screen;
    gint width, height;
    screen = gdk_screen_get_default();
    width  = gdk_screen_get_width(screen);
    height = gdk_screen_get_height(screen);

    GtkWidget *fixed, *btnyes, *label;
    g_start_window->show_wait_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_modal((GtkWindow *)g_start_window->show_wait_win, TRUE);
    gtk_window_set_keep_above(GTK_WINDOW(g_start_window->show_wait_win), TRUE);
    gtk_window_set_position(GTK_WINDOW(g_start_window->show_wait_win), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_window_set_default_size(GTK_WINDOW(g_start_window->show_wait_win), width, height);
    gtk_window_set_decorated(GTK_WINDOW(g_start_window->show_wait_win), FALSE);//无边框

    btnyes = gtk_button_new_with_label("取消");
    gtk_widget_set_size_request(btnyes, 103, 20);
    gtk_button_set_relief(GTK_BUTTON(btnyes), GTK_RELIEF_NONE);
    label = gtk_label_new("正在连接，请稍后...");

    fixed = gtk_fixed_new();
    gtk_fixed_put((GtkFixed *)fixed, label,  width / 2 - 55, height / 2);
    gtk_fixed_put((GtkFixed *)fixed, btnyes, width / 2 - 55, height / 2 + 40);
    gtk_container_add(GTK_CONTAINER(g_start_window->show_wait_win), fixed);

    g_signal_connect(G_OBJECT(btnyes), "clicked", G_CALLBACK(cancel_auto_login), NULL);

    set_start_window_show_wait_css(btnyes);

    gtk_widget_show_all(g_start_window->show_wait_win);
}

gboolean get_ip_node(void)
{
#ifdef __linux__
    char tmp_buf[50];
    char ip_str[32] = {0};
    char strIpAndNode[150] = "";

    int ret = 0;
    if (get_local_ip(ip_str) == 0) {
        strcat(strIpAndNode, "有线:");
        strcat(strIpAndNode, ip_str);
    } else {
        ret = get_wifi_now(tmp_buf);
        if (ret == CONNECTED) {
            strcat(strIpAndNode, "无线:");
            strcat(strIpAndNode, tmp_buf);
        } else if (ret == CONNECTING) {
            strcat(strIpAndNode, "无线:");
            strcat(strIpAndNode, "正在获取网络");
        } else if (ret == DISCONNECT) {
            strcat(strIpAndNode, "无线:");
            strcat(strIpAndNode, "未连接");
        } else {
            strcat(strIpAndNode, "有线:");
            strcat(strIpAndNode, "无连接");
        }
    }


    strcat(strIpAndNode, "  节点:");

    struct vcd_conf* cf = g_vcd_conf;

    char *txt_node = cf->ip;
    if (strlen(txt_node) == 0)
        strcat(strIpAndNode, "未设置");
    else
        strcat(strIpAndNode, cf->ip);

    if(g_start_window->label_ip_node)
    gtk_label_set_text((GtkLabel *)g_start_window->label_ip_node, strIpAndNode);
#endif
    return TRUE;
}

void open_interval_get_ip(void)
{
#ifdef __linux__
     g_timeout_ip_node = g_timeout_add_seconds(2, (GSourceFunc)get_ip_node, NULL);
#endif
}

void stop_interval_get_ip(void)
{
    if (g_timeout_ip_node != 0) {
        g_source_remove(g_timeout_ip_node);
        g_timeout_ip_node = 0;
    }
}

static gboolean portal_token_callback(gpointer response){

    struct network_response* res = (struct network_response*)response;

    char *memory = res->memory;
    if (!response || !memory) {
        //return FALSE;
    }
    if (res->error_msg != NULL) {
        LOG_ERROR("portal token get failed: %s", res->error_msg);
        //return FALSE;
    }
    struct vcd_conf* tmp_vcd_conf = g_vcd_conf;
    char portal_token[100] = {0};
    snprintf(portal_token, 100, "X-CSRF-Token:%s", memory);
    M_DEBUG("portal_token:%s", portal_token);
    if (vcd_webkit_login(portal_token, tmp_vcd_conf->ip, tmp_vcd_conf->username, tmp_vcd_conf->plaintext_passwd, g_start_window) == -1) {
        LOG_ERROR("start get ticket error");
        message_box(" 获取登录ticket失败 ");
    }

    network_response_free(&res);
    return FALSE;
}

static int portal_get_token(const char* host)
{
    struct network_request_arg *req = (struct network_request_arg *)calloc(1, sizeof(struct network_request_arg));
    if (req == NULL) {
        LOG_ERROR("malloc network_req_arg error");
        return -1;
    }
    snprintf(req->url, sizeof(req->url), "https://%s/session/token", host);

    req->timeout = 5L;
    req->data_priv = NULL;
    req->callback = portal_token_callback;

    net_req_list_add(req);

    start_login_button_state();

    return 0;
}

static
void cb_button_login(void)
{
    struct vcd_conf* tmp_vcd_conf = g_vcd_conf;
    if (g_logined_window && g_logined_window->logined_window_handle != NULL)
        exit_logined_window();
#ifndef __linux__
    strcpy(tmp_vcd_conf->ip, gtk_entry_get_text((GtkEntry *)g_start_window->entry_node));
#endif
    strcpy(tmp_vcd_conf->username, gtk_entry_get_text(GTK_ENTRY(g_start_window->entry_user)));
    strcpy(tmp_vcd_conf->plaintext_passwd, gtk_entry_get_text(GTK_ENTRY(g_start_window->entry_passwd)));

    char encode_out[1024] = {0};
    vcd_encode_crypto(tmp_vcd_conf->plaintext_passwd, encode_out);

    /* 若记住密码，则将密码复制到配置文件 ，若不是记住密码则将配置文件密码项清零*/
    if (gtk_toggle_button_get_active((GtkToggleButton *)g_start_window->remember_passwd))
        strncpy(tmp_vcd_conf->passwd, encode_out, sizeof(tmp_vcd_conf->passwd));
    else
        memset(tmp_vcd_conf->passwd, 0, sizeof(tmp_vcd_conf->passwd));

    /* 存储用户名和密码 */
    update_vcd_conf();

    if (if_support_local_model() && tmp_vcd_conf->local_model_check_status) {


        if (vraify_localmode_user_pass(tmp_vcd_conf->username, tmp_vcd_conf->plaintext_passwd)) {
            M_DEBUG("local mode login success");
            login_as_local_model();
        } else {
            message_box("登录失败，用户名或密码错误");
            M_DEBUG("local mode login failed");
        }
    } else {
        /* 若用户名密码为空，则提示返回 */
        if (strlen(tmp_vcd_conf->ip) == 0) {
            message_box(" 请输入服务器ip！ ");
            return;
        }
        if (strlen(tmp_vcd_conf->username) == 0) {
            message_box(" 请输入用户名！ ");
            return;
        }
        if (strlen(tmp_vcd_conf->plaintext_passwd) == 0) {
            message_box(" 请输入密码！ ");
            return;
        }
        portal_get_token(tmp_vcd_conf->ip);
    }
}

void start_login_button_state(void)
{
    g_return_if_fail(g_start_window != NULL);
    g_return_if_fail(g_start_window->button_login != NULL);
    gtk_button_set_label((GtkButton *)g_start_window->button_login, "正在登录...");
    gtk_widget_set_sensitive(g_start_window->button_login, FALSE);
}

void stop_login_button_state(void)
{
    g_return_if_fail(g_start_window != NULL);
    g_return_if_fail(g_start_window->button_login != NULL);
    gtk_button_set_label((GtkButton *)g_start_window->button_login, "登录");
    gtk_widget_set_sensitive(g_start_window->button_login, TRUE);
}

static void cb_entry_backspace(GtkWidget *entry) 
{
    M_DEBUG("num = %d", (int)strlen(gtk_entry_get_text(GTK_ENTRY(entry))));
    if (strlen(gtk_entry_get_text(GTK_ENTRY(entry))) == 0) {//删除最后一个
        g_icon_press_state = g_signal_connect(G_OBJECT(entry), "icon-press", G_CALLBACK(cb_entry_icon_press), NULL);
        M_DEBUG("g_icon_press_state = %d", g_icon_press_state);
    }
}

static void cb_entry_icon_press(GtkWidget *entry)
{
    GError *error = NULL;
    GdkPixbuf *pixbuf = NULL;
    if (gtk_entry_get_visibility(GTK_ENTRY(entry))) {
        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
        gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(entry),
                                          GTK_ENTRY_ICON_SECONDARY, "显示密码");
        pixbuf = gdk_pixbuf_new_from_file(VCD_PIC_PATH"closeeye.png", &error);
        if (!pixbuf) {
            LOG_ERROR(error->message);
            g_error_free(error);
        }
        gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(entry), GTK_ENTRY_ICON_SECONDARY, pixbuf);
    } else {
        gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
        gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(entry),
                                          GTK_ENTRY_ICON_SECONDARY, "隐藏密码");
        pixbuf = gdk_pixbuf_new_from_file(VCD_PIC_PATH"openeye.png", &error);
        if (!pixbuf) {
            LOG_ERROR(error->message);
            g_error_free(error);
        }
        gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(entry), GTK_ENTRY_ICON_SECONDARY, pixbuf);
    }
}

void set_passwd_entry_eye(GtkWidget *entry, gboolean openeye)
{
    GError *error = NULL;
    GdkPixbuf *pixbuf = NULL;
    if (openeye) {
        gtk_entry_set_visibility(GTK_ENTRY(entry), openeye);

        gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(entry),
                                          GTK_ENTRY_ICON_SECONDARY, "隐藏密码");
        pixbuf = gdk_pixbuf_new_from_file(VCD_PIC_PATH"openeye.png", &error);
        if (!pixbuf) {
            LOG_ERROR(error->message);
            g_error_free(error);
        }
        gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(entry), GTK_ENTRY_ICON_SECONDARY, pixbuf);
    } else {
        gtk_entry_set_visibility(GTK_ENTRY(entry), openeye);

        gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(entry),
                                          GTK_ENTRY_ICON_SECONDARY, "显示密码");
        pixbuf = gdk_pixbuf_new_from_file(VCD_PIC_PATH"closeeye.png", &error);
        if (!pixbuf) {
            LOG_ERROR(error->message);
            g_error_free(error);
        }
        gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(entry), GTK_ENTRY_ICON_SECONDARY, pixbuf);
    }
}

void vcd_client_start(void)
{
    read_vcd_conf();

    g_start_window = g_malloc(sizeof(struct start_window));
    g_return_if_fail(g_start_window != NULL);
    memset(g_start_window, 0, sizeof(struct start_window));

    struct vcd_conf* tmp_vcd_conf = g_vcd_conf;
    if (!tmp_vcd_conf) {
        LOG_ERROR("not found vcd-conf");
        return;
    }

    GtkWidget *button_quit, *btn_sys_set, *fixed_login;
    GtkWidget *label_none, *label_version;
    GtkWidget *grid;

    g_start_window->main_window_handle = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_resizable(GTK_WINDOW(g_start_window->main_window_handle), FALSE);//窗口大小不可改变
    gtk_container_set_border_width(GTK_CONTAINER(g_start_window->main_window_handle), 15);//设置整个屏幕周围空出来的的空间
    gtk_window_set_position( GTK_WINDOW(g_start_window->main_window_handle), GTK_WIN_POS_CENTER_ALWAYS);//居中
#ifdef __linux__
    gtk_window_set_decorated(GTK_WINDOW(g_start_window->main_window_handle), FALSE);//无边框（就是最大化最小化关闭的那一栏）
#else
    g_start_window->header = gtk_header_bar_new();
    gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(g_start_window->header), TRUE);
    gtk_header_bar_set_title(GTK_HEADER_BAR(g_start_window->header), "");
    gtk_widget_set_size_request(g_start_window->header, -1, 55);
    gtk_window_set_titlebar(GTK_WINDOW(g_start_window->main_window_handle), g_start_window->header);
    //g_signal_connect(G_OBJECT(g_start_window->main_window_handle), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
    //vcd_common_set_app_icon(g_start_window->main_window_handle, VCD_PIC_PATH"Vclient.ico");
    //g_signal_connect(g_start_window->main_window_handle, "destroy", G_CALLBACK(gtk_main_quit), NULL);
#endif
    grid = gtk_grid_new();
    gtk_grid_set_row_homogeneous((GtkGrid *)grid, FALSE);//grid的每行大小可不相同
    gtk_grid_set_column_homogeneous((GtkGrid *)grid, FALSE);//列大小可不相同
    gtk_grid_set_row_spacing((GtkGrid*)grid, 33);//行距

    g_start_window->button_login = gtk_button_new_with_label("登  录");
    gtk_widget_set_size_request(g_start_window->button_login, 272, 15);

    g_start_window->remember_passwd = gtk_check_button_new_with_label("记住密码");
    g_start_window->auto_login      = gtk_check_button_new_with_label("自动连接");
    g_start_window->local_model     = gtk_check_button_new_with_label("本地模式");

    gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->local_model, g_vcd_conf->local_model_check_status);
    if (g_vcd_conf->local_model_check_status)
        prepare_local_model();

    btn_sys_set = gtk_button_new();//设置按钮
    gtk_button_set_relief(GTK_BUTTON(btn_sys_set), GTK_RELIEF_NONE);//设置按钮无边框
    gtk_widget_set_size_request(btn_sys_set, 80, 34);

    button_quit = gtk_button_new();//退出按钮
    gtk_button_set_relief(GTK_BUTTON(button_quit), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(button_quit, 80, 34);

    label_none = gtk_label_new(NULL);//放在最顶上的

    g_start_window->entry_user = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(g_start_window->entry_user), 32);
    gtk_widget_set_size_request(g_start_window->entry_user, 185, 28);

    M_DEBUG("read config username = %s", tmp_vcd_conf->username);
    if (strlen(tmp_vcd_conf->username) != 0) {
        gtk_entry_set_text((GtkEntry *)g_start_window->entry_user, tmp_vcd_conf->username);
    } else {
        gtk_entry_set_text((GtkEntry *)g_start_window->entry_user, "");
    }

    g_start_window->entry_passwd = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(g_start_window->entry_passwd), 32);
    gtk_widget_set_size_request(g_start_window->entry_passwd, 185, 28);
    gtk_entry_set_visibility((GtkEntry *)g_start_window->entry_passwd, FALSE);
    GError *error = NULL;
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(VCD_PIC_PATH"closeeye.png", &error);
    if (!pixbuf) {
        LOG_ERROR(error->message);
        g_error_free(error);
    }
    gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(g_start_window->entry_passwd),
                                   GTK_ENTRY_ICON_SECONDARY, pixbuf);
    gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(g_start_window->entry_passwd),
                                      GTK_ENTRY_ICON_SECONDARY, "显示密码");
    //char aes_decode_out[500] = {0};
    //char base64_decode_out[1024] = {0};
    char decode_out[1024] = {0};

    M_DEBUG("read config passwd = %s", tmp_vcd_conf->passwd);
    if (strlen(tmp_vcd_conf->passwd) != 0) {

        vcd_decode_crypto(tmp_vcd_conf->passwd, decode_out);
//        M_DEBUG("str_passwd:%s", tmp_vcd_conf->passwd);
//        base64_decode(tmp_vcd_conf->passwd, strlen(tmp_vcd_conf->passwd), base64_decode_out);
//        M_DEBUG("base64_decode_out:%s\n", base64_decode_out);
//        aes_decrypt_extend(base64_decode_out,sizeof(aes_decode_out)-1, aes_decode_out);
        //M_DEBUG("aes_decode_out:%s\n", decode_out);
        gtk_entry_set_text((GtkEntry *)g_start_window->entry_passwd, decode_out);
        gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->remember_passwd, TRUE);
    } else {
        gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->remember_passwd, FALSE);
        gtk_entry_set_text((GtkEntry *)g_start_window->entry_passwd, "");
    }

    M_DEBUG("read config enable-autologin = %d", tmp_vcd_conf->enable_autologin);
    if (tmp_vcd_conf->enable_autologin == 1) {
        if (!gtk_toggle_button_get_active((GtkToggleButton *)g_start_window->remember_passwd)) {
            gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->auto_login, FALSE);
        } else {
            gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->auto_login, TRUE);
        }
    } else {
        gtk_toggle_button_set_active((GtkToggleButton *)g_start_window->auto_login, FALSE);
    }

    fixed_login = gtk_fixed_new();

#ifndef __linux__
    g_start_window->entry_node = gtk_entry_new();
    gtk_widget_set_size_request(g_start_window->entry_node, 185, 28);
    gtk_entry_set_placeholder_text(GTK_ENTRY(g_start_window->entry_node), "节点");

    M_DEBUG("read config ip = %s", tmp_vcd_conf->ip);
    if (strlen(tmp_vcd_conf->ip) != 0) {
        gtk_entry_set_text((GtkEntry *)g_start_window->entry_node, tmp_vcd_conf->ip);
    } else {
        gtk_entry_set_text((GtkEntry *)g_start_window->entry_node, "");
    }

    g_start_window->login_type = gtk_button_new();
    gtk_widget_set_size_request(g_start_window->login_type, 28, 28);
    gtk_button_set_relief(GTK_BUTTON(g_start_window->login_type), GTK_RELIEF_NONE);//设置无按钮边框

    char tmp_version[25] = {0};
    strncpy(tmp_version, PACKAGE_VERSION, 8);
    label_version = gtk_label_new(tmp_version);

    gtk_fixed_put((GtkFixed *)fixed_login, label_none, 416, 95);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->entry_user, 129, 56);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->entry_passwd, 129, 90);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->remember_passwd, 74, 145);
    //gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->auto_login, 250, 145);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->button_login, 72, 184);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->entry_node, 129, 22);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->login_type, 73, 21);
    gtk_fixed_put((GtkFixed *)fixed_login, label_version, 360, 205);

    g_signal_connect_swapped(G_OBJECT(g_start_window->main_window_handle), "destroy", G_CALLBACK(exit), 0);
#else
    if (!tmp_vcd_conf->enable_user_passwd) {
        M_DEBUG("disable white entry user and password");
        gtk_widget_set_sensitive(g_start_window->entry_passwd, FALSE);
        gtk_widget_set_sensitive(g_start_window->remember_passwd, FALSE);
        gtk_widget_set_sensitive(g_start_window->entry_user, FALSE);
        gtk_widget_set_sensitive(g_start_window->auto_login, FALSE);
    }
    
    g_start_window->label_ip_node = gtk_label_new(NULL);

    get_ip_node();
    gtk_widget_set_size_request(g_start_window->label_ip_node, 419, 0);
    g_timeout_ip_node = g_timeout_add_seconds(2, (GSourceFunc)get_ip_node, NULL);
    
    gtk_fixed_put((GtkFixed *)fixed_login, btn_sys_set, 83, 222);
    gtk_fixed_put((GtkFixed *)fixed_login, button_quit, 247, 222);
    gtk_fixed_put((GtkFixed *)fixed_login, label_none, 415, 62);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->entry_user, 134, 77);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->entry_passwd, 134, 110);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->remember_passwd, 69, 160);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->local_model, 266, 160);
    gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->button_login, 72, 184);

    if (g_start_window->label_ip_node)
        gtk_fixed_put((GtkFixed *)fixed_login, g_start_window->label_ip_node, 0, 258);

    PasswdContext* pc_setting = g_malloc0(sizeof(PasswdContext));
    pc_setting->check_func = check_system_passwd;
    pc_setting->ok_func = create_setting_window;

    g_signal_connect_swapped(G_OBJECT(button_quit), "clicked", G_CALLBACK(cb_button_quit), NULL);
    g_signal_connect_swapped(G_OBJECT(btn_sys_set), "clicked", G_CALLBACK(get_sys_passwd_ui), (gpointer)pc_setting);

    if (gtk_toggle_button_get_active((GtkToggleButton *)g_start_window->auto_login)) {
        show_wait();
        g_timeout_wait_ip = g_timeout_add(500, (GSourceFunc)wait_ip, NULL);
    }
#endif

    g_signal_connect(G_OBJECT(g_start_window->remember_passwd), "toggled", G_CALLBACK(cb_remember_passwd), NULL);
    g_signal_connect(G_OBJECT(g_start_window->button_login), "clicked", G_CALLBACK(cb_button_login), NULL);
    g_signal_connect(G_OBJECT(g_start_window->entry_passwd), "activate", G_CALLBACK(cb_button_login), NULL);
    g_signal_connect(G_OBJECT(g_start_window->entry_user), "activate", G_CALLBACK(cb_button_login), NULL);
    g_signal_connect(G_OBJECT(g_start_window->auto_login), "toggled", G_CALLBACK(cb_auto_login), NULL);
    g_signal_connect(G_OBJECT(g_start_window->entry_passwd), "changed", G_CALLBACK(cb_entry_backspace), NULL);
    g_signal_connect(G_OBJECT(g_start_window->local_model), "toggled", G_CALLBACK(cb_local_model), NULL);

    //安全起见第一次是不让显示密码的
    //g_signal_connect(G_OBJECT(g_start_window->entry_passwd), "icon-press", G_CALLBACK(cb_entry_icon_press), NULL);

    gtk_widget_set_can_focus(g_start_window->button_login, TRUE);//将焦点放在登录按钮上
    gtk_widget_grab_focus(g_start_window->button_login);

    gtk_grid_attach((GtkGrid *)grid, fixed_login, 0, 0, 1, 1);
    gtk_container_add(GTK_CONTAINER(g_start_window->main_window_handle), grid);
    gtk_widget_show_all(g_start_window->main_window_handle);

    set_start_window_css(button_quit, btn_sys_set);
}

void vcd_start_window_show(){
    g_return_if_fail(g_start_window!=NULL);

    if(!gtk_widget_is_visible(g_start_window->main_window_handle)){
        gtk_widget_show_all(g_start_window->main_window_handle);
    }
}
void vcd_start_window_hide(){
    g_return_if_fail(g_start_window!=NULL);

    if(gtk_widget_is_visible(g_start_window->main_window_handle)){
        gtk_widget_hide(g_start_window->main_window_handle);
    }
}

void vcd_start_window_set_full_screen(gboolean b) {
    if (g_start_window)
        g_start_window->full_screen = b;
}

void vcd_start_window_set_open_develop_tool(gboolean b) {
    if (g_start_window)
        g_start_window->open_develop_tool = b;

}

static gboolean if_support_local_model(){
    #ifdef __linux__
    if(!access(PVE_API_TOKEN_CONF, F_OK)){
        return TRUE;
    }
    #endif
    return FALSE;
}

static void prepare_local_model(){
#ifdef __linux__

    if (!if_support_local_model()) {

        //创建账户
        system("pveum groupadd admin -comment \"System Administrators\"");
        system("pveum useradd wsagent@pve -comment \"wsagent user\"");
        system("pveum usermod wsagent@pve -group admin");
        system("pveum user token add wsagent@pve IDV -privsep 1");
        system("pveum aclmod / -group admin -role Administrator");
        system("pveum aclmod / -token 'wsagent@pve!IDV' -role Administrator");
    }

    start_localmode_heartbeat();

#endif
}

static void set_local_model_status(gboolean is_local_model){

    if (is_local_model) {
        prepare_local_model();
    } else {
        destroy_heartbeat();
    }
    if (g_vcd_conf) {
        g_vcd_conf->local_model_check_status = is_local_model;
        update_vcd_conf();
    }
}

static void login_as_local_model(){

    if (1) {
        main_logined_window(TRUE);
    } else {
        //const char* unix_file = g_start_window->unix_path;
        const char* unix_file = "/var/run/qemu-server/spice-100.sock";
        spicy_connect_with_unix(unix_file);
    }
}
